package hr.ctg.web.servlet;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Serves images and other member content
 * 
 * @author Shane Bryzak
 */
public class SuggestContentServlet extends HttpServlet {

	private static final long serialVersionUID = -8461940507241022247L;

	@Override
	public void init() {

	}

	@Override
	protected void doGet(final HttpServletRequest request,
			final HttpServletResponse response) throws ServletException,
			IOException {

		final String articleId = request.getParameter("articleId");
		final String groupId = request.getParameter("groupId");
		analyzeRequest(request);
		response.setContentType("text/xml");
		PrintWriter out = response.getWriter();
		if (null != articleId) {

			System.out.println("Parametar je: " + articleId);
			System.out.println("Prosljedjujem zahtjev dalje");
			URL url = new URL(
					"http://www.crotouristguide.com/c/journal/get_article?groupId="
							+ (null == groupId ? "273" : groupId)
							+ "&articleId=" + articleId);
			copyRequestHeaders(request, url);
			BufferedReader br = new BufferedReader(new InputStreamReader(url
					.openStream()));
			String line;
			while (null != (line = br.readLine())) {
				// System.out.println(line);
				out.println(line);
			}
			br.close();
			/*
			 * request.getRequestDispatcher(
			 * "http://localhost:9080/c/journal/get_article").forward( request,
			 * response);
			 */
		} else {
			out
					.print("<root><content-resource>empty</content-resource></root>");
		}
		out.flush();
		out.close();
		System.out.println("Zahtjev obradjen vracam se...");

	}

	@SuppressWarnings("unchecked")
	void copyRequestHeaders(final HttpServletRequest request, URL url)
			throws IOException {
		System.out.println("kopiram request headere...");
		URLConnection urlConn = url.openConnection();
		Enumeration<String> headers = request.getHeaderNames();
		while (headers.hasMoreElements()) {
			String header = headers.nextElement();
			urlConn.setRequestProperty(header, request.getHeader(header));
			System.out.println(header + "-" + request.getHeader(header));
		}
	}

	@SuppressWarnings("unchecked")
	void analyzeRequest(final HttpServletRequest request) {
		System.out.println(request.getAttributeNames());
		System.out.println(request.getCharacterEncoding());
		System.out.println(request.getContentType());
		System.out.println(request.getContextPath());
		System.out.println(request.getHeaderNames());
		Enumeration<String> headers = request.getHeaderNames();
		while (headers.hasMoreElements()) {
			String header = headers.nextElement();
			System.out.println(header + "-" + request.getHeader(header));
		}
		System.out.println(request.getLocale());
	}
}
